﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QL_LICHLAMVIEC.Models;

namespace QL_LICHLAMVIEC.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {



            return View(LoadTaiKhoan());
        }


        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            MyEventsDataContext data = new MyEventsDataContext();
            //     try
            //  {
            if (ModelState.IsValid)
            {
                GIANGVIEN_NHANVIEN gv = new GIANGVIEN_NHANVIEN
                {
                    MSNV = int.Parse(collection["MSNV"]),
                    Hoten = collection["hoten"],
                    ngay_sinh = DateTime.Parse(collection["ngaysinh"]),
                    Chuc_vu = collection["chucvu"],
                    Dien_thoai = collection["dienthoai"]
                };

                data.GIANGVIEN_NHANVIENs.InsertOnSubmit(gv);
                //    data.SubmitChanges();

                USER u = new USER
                {
                    MSNV = int.Parse(collection["MSNV"]),
                    Username = collection["username"],
                    Password = collection["password"],
                    Email = collection["email"]

                };

                u.GIANGVIEN_NHANVIEN = gv; // inner jon 2 bang

                data.USERs.InsertOnSubmit(u);

                data.SubmitChanges();
            }
            ViewData["msg"] = "Đã nhập thành công.";
            //  }
            //catch
            //{
            //    ViewData["msg"] = "Lỗi trong khi nhập.";
            //}

            return View(LoadTaiKhoan());
        }
        private List<TaiKhoan> LoadTaiKhoan()
        {
            MyEventsDataContext data = new MyEventsDataContext();
            var taikhoan = data.SP_TaiKhoan().ToList();
            List<TaiKhoan> List_TK = new List<TaiKhoan>();
            for (int i = 0; i < taikhoan.Count; i++)
            {
                var tk = new TaiKhoan()
                {

                    MSNV = taikhoan[i].MSNV,
                    HoTen = taikhoan[i].Hoten,
                    NgaySinh = DateTime.Parse(taikhoan[i].ngay_sinh.ToString()),
                    Chucvu = taikhoan[i].Chuc_vu,
                    DienThoai = taikhoan[i].Dien_thoai,
                    UserName = taikhoan[i].Username,
                    Password = taikhoan[i].Password,
                    Email = taikhoan[i].Email
                };
                List_TK.Add(tk);
            }
            return List_TK;
        }
    }
}
